package com.house.framework.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: WebServiceApi
 * @description:
 * @author: huangyl
 * @create: 2022-09-23 11:10
 **/
public class TreeUtils {


    private List<JSONObject> treeList;

    /**
     * 生成下拉树
     * @param data
     * @param key
     * @param keyName
     * @param pKey
     * @param pvalue
     * @return
     */
    public List<Object> treeList(List<JSONObject> data,String key,String keyName,String pKey,String pvalue) {
        this.treeList = data;
        List<Object> list = new ArrayList<>();
        for (JSONObject ps : data) {
            JSONObject map = new JSONObject();
            //判断根节点是否是0
            boolean flang = false;
            if (pvalue.equals(ps.getString(pKey))) {
                flang=true;
            }
            if(flang){
                setTreeData(map, ps,key,keyName,pKey,pvalue);
                list.add(map);
            }

        }
        return list;
    }

    /**
     * 子集查找遍历
     * 递归遍历，直到没有子节点为止
     *
     * @return list
     */
    private List<Object> treeChild(String pValue,String key,String nameKey,String pKey) {
        List<Object> list = new ArrayList<>();
        for (JSONObject ps : treeList) {
            JSONObject map = new JSONObject();
            if (pValue.equals(ps.getString(pKey))) {
                setTreeData(map, ps,key,nameKey,pKey,pValue);
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 赋值
     * id  当前id
     * title 标题
     * field 权限值
     * href 跳转路径
     * children 子节点数据
     */
    private void setTreeData(JSONObject map, JSONObject ps,String key,String nameKey,String pKey,String pValue) {
        map.put("id", ps.getString(key));
        map.put("pid",pValue);
        map.put("label", ps.getString(nameKey));
        map.put("name", ps.getString(nameKey));
        map.put("attributes",ps);
        List<Object> children = treeChild(ps.getString(key), key, nameKey, pKey);
        if(children.size()>0){
            map.put("children", children);
        }

    }


    public static List<Map<String, Object>> getTree(List<Map<String, Object>> data) {
        // 处理的数据存在
        List<Map<String, Object>> allList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> pList = new ArrayList<Map<String, Object>>();// eTree
        // 数据处理
        for (Map<String, Object> tmp : data) {
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("id", tmp.get("ID"));
            map.put("pId", String.valueOf(tmp.get("PID"))=="null"?"":tmp.get("PID"));
            map.put("name", tmp.get("NM"));
            map.put("user", false);
            map.put("children", new ArrayList<>());
            map.put("level", 0);
            allList.add(map);
        }
        // 获取父节点
        for (Map<String, Object> tmp : allList) {
            String cg_pig = String.valueOf(tmp.get("pId"));
            if (StringUtils.isEmpty(cg_pig)) {
                // 组装子节点，开始循环去获取子节点
                tmp.put("children", menuChild(String.valueOf(tmp.get("id")), allList,0));
                pList.add(tmp);
            }
        }
        return pList;
    }
    /**
     * 处理子节点
     * @param id
     * @param data
     * @return
     */
    public static List<?> menuChild(String id, List<Map<String, Object>> data,Integer level) {
        List<Object> lists = new ArrayList<Object>();
        for (Map<String, Object> map : data) {
            String cg_pig = String.valueOf(map.get("pId"));
            if (cg_pig.equals(id)) {
                // 组装子节点，开始循环去获取子节点
                map.put("level",level+1);
                map.put("children", menuChild(String.valueOf(map.get("id")), data,level+1));
                if(((List)map.get("children")).size()==0){
                    map.put("user",true);
                }
                lists.add(map);
            }
        }
        return lists;
    }

}
